package cn.sccdlg.ssm.dao;

import cn.sccdlg.ssm.domain.Member;
import cn.sccdlg.ssm.domain.Order;
import cn.sccdlg.ssm.domain.Product;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author RaoShuang
 * @emil 991207823@qq.com
 * @date 2020/10/27 21:15
 * 订单持久层
 */
@Repository
@Mapper
public interface IOrderDao {
    /**
     * 查询所有订单
     */
    @Select("SELECT * FROM `order`")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "orderNum", column = "orderNum"),
            @Result(property = "orderTime", column = "orderTime"),
            @Result(property = "orderStatus", column = "orderStatus"),
            @Result(property = "peopleCount", column = "peopleCount"),
            @Result(property = "peopleCount", column = "peopleCount"),
            @Result(property = "payType", column = "payType"),
            @Result(property = "orderDesc", column = "orderDesc"),
            @Result(property = "product", column = "productId", javaType = Product.class,
                    one = @One(select = "cn.sccdlg.ssm.dao.IProductDao.findById")),
    })
    List<Order> findAll() throws Exception;

    /**
     * 查询所有订单-未分页
     */
    @Select("SELECT * FROM `order`")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "orderNum", column = "orderNum"),
            @Result(property = "orderTime", column = "orderTime"),
            @Result(property = "orderStatus", column = "orderStatus"),
            @Result(property = "peopleCount", column = "peopleCount"),
            @Result(property = "peopleCount", column = "peopleCount"),
            @Result(property = "payType", column = "payType"),
            @Result(property = "orderDesc", column = "orderDesc"),
            @Result(property = "product", column = "productId", javaType = Product.class,
                    one = @One(select = "cn.sccdlg.ssm.dao.IProductDao.findById")),
    })
    List<Order> findAllNonPaged() throws Exception;

    /**
     * 多表操作，根据订单id查询订单
     * @param ordersId 订单id
     * @return
     * @throws Exception
     */
    @Select("SELECT * FROM `order` WHERE id=#{orderId}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "orderNum", column = "orderNum"),
            @Result(property = "orderTime", column = "orderTime"),
            @Result(property = "orderStatus", column = "orderStatus"),
            @Result(property = "peopleCount", column = "peopleCount"),
            @Result(property = "peopleCount", column = "peopleCount"),
            @Result(property = "payType", column = "payType"),
            @Result(property = "orderDesc", column = "orderDesc"),
            @Result(property = "product", column = "productId", javaType = Product.class,
                    one = @One(select = "cn.sccdlg.ssm.dao.IProductDao.findById")),
            @Result(property = "member",column = "memberId",javaType = Member.class,
                    one = @One(select = "cn.sccdlg.ssm.dao.IMemberDao.findById")),
            @Result(property = "travellers",column = "id",javaType =java.util.List.class,
                    many = @Many(select = "cn.sccdlg.ssm.dao.ITravellerDao.findByOrdersId"))
    })
    Order findById(String orderId) throws Exception;
}
